package online.shenjian.hadoop.outputformat;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

/**
 * @author shenjian
 * @since 2023/2/18
 */
public class LogRecordWriter extends RecordWriter<Text, NullWritable> {

    private FSDataOutputStream dataOutputStreamOne;
    private FSDataOutputStream dataOutputStreamTwo;

    public LogRecordWriter(TaskAttemptContext job) {
        try {
            FileSystem fileSystem = FileSystem.get(job.getConfiguration());
            dataOutputStreamOne = fileSystem.create(new Path("hadoop/src/main/resources/site/shenjian.log"));
            dataOutputStreamTwo = fileSystem.create(new Path("hadoop/src/main/resources/site/other.log"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void write(Text key, NullWritable value) throws IOException, InterruptedException {
        String log = key.toString();
        if (log.contains("shenjian.online")) {
            dataOutputStreamOne.writeBytes(log + "\n");
        } else {
            dataOutputStreamTwo.writeBytes(log + "\n");
        }
    }

    @Override
    public void close(TaskAttemptContext context) throws IOException, InterruptedException {
        IOUtils.closeStream(dataOutputStreamOne);
        IOUtils.closeStream(dataOutputStreamTwo);
    }
}
